package com.sl.biz.modular.cdc.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.biz.modular.cdc.controller.domain.request.CdcDatabaseGroupRequest;
import com.sl.biz.modular.cdc.controller.domain.request.CdcTableConfigGroupRequest;
import com.sl.biz.modular.cdc.dao.domain.entity.CdcColumnConfigDO;
import com.sl.biz.modular.cdc.dao.domain.entity.CdcDatabaseConfigDO;
import com.sl.biz.modular.cdc.dao.domain.entity.CdcTableConfigDO;
import com.sl.biz.modular.cdc.dao.service.CdcColumnConfigDaoService;
import com.sl.biz.modular.cdc.dao.service.CdcDatabaseConfigDaoService;
import com.sl.biz.modular.cdc.dao.service.CdcTableConfigDaoService;
import com.sl.biz.modular.cdc.service.CdcDatabaseBizService;
import com.sl.biz.modular.cdc.service.domain.dto.CdcColumnConfigDTO;
import com.sl.biz.modular.cdc.service.domain.dto.CdcDatabaseGroupDTO;
import com.sl.biz.modular.cdc.service.domain.dto.CdcTableConfigGroupDTO;
import com.sl.common.pojo.CommonEntity;
import com.sl.core.engine.utils.BeanUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CdcDatabaseBizServiceImpl implements CdcDatabaseBizService {
    private final CdcDatabaseConfigDaoService databaseConfigDaoService;
    private final CdcTableConfigDaoService tableConfigDaoService;
    private final CdcColumnConfigDaoService cdcColumnConfigDaoService;

    @Override
    public List<CdcDatabaseGroupDTO> queryByCdcId(CdcDatabaseConfigDO jobConfigDO) {
        List<CdcDatabaseConfigDO> cdcDatabaseConfigDOS = databaseConfigDaoService.queryByCdcId(jobConfigDO.getCdcId());

        if (CollUtil.isEmpty(cdcDatabaseConfigDOS)) {
            return Collections.emptyList();
        }
        List<CdcDatabaseGroupDTO> databaseGroupDTOS = BeanUtil.copyToList(cdcDatabaseConfigDOS, CdcDatabaseGroupDTO.class);

        List<String> databaseIds = databaseGroupDTOS.stream().map(CommonEntity::getId).toList();

        List<CdcTableConfigDO> cdcTableConfigDOList = tableConfigDaoService.queryByDatabaseIds(databaseIds);
        if (CollUtil.isEmpty(cdcTableConfigDOList)) {
            return databaseGroupDTOS;
        }
        List<String> tableIds = cdcTableConfigDOList.stream().map(CommonEntity::getId).toList();
        List<CdcColumnConfigDO> cdcColumnConfigDOList = cdcColumnConfigDaoService.queryByTableIds(tableIds);

        for (CdcDatabaseGroupDTO databaseGroupDTO : databaseGroupDTOS) {
            String id = databaseGroupDTO.getId();
            List<CdcTableConfigGroupDTO> list = cdcTableConfigDOList.stream().filter(v -> StrUtil.equals(v.getCdcDatabaseId(), id))
                    .map(v -> {
                        CdcTableConfigGroupDTO cdcTableConfigGroupDTO = BeanUtil.copyProperties(v, CdcTableConfigGroupDTO.class);

                        List<CdcColumnConfigDTO> list1 = cdcColumnConfigDOList.stream().filter(vv -> StrUtil.equals(vv.getCdcTableId(), v.getId()))
                                .map(vv -> {
                                    CdcColumnConfigDTO cdcColumnConfigDTO = BeanUtil.copyProperties(vv, CdcColumnConfigDTO.class);
                                    return cdcColumnConfigDTO;
                                })
                                .toList();
                        cdcTableConfigGroupDTO.setCdcColumnConfigList(list1);
                        return cdcTableConfigGroupDTO;
                    }).toList();
            databaseGroupDTO.setTableList(list);

        }

        return databaseGroupDTOS;
    }

    @Override
    public Boolean removeByCdcId(String cdcId) {

        databaseConfigDaoService.removeByCdcId(cdcId);
        tableConfigDaoService.removeByCdcId(cdcId);
        cdcColumnConfigDaoService.removeByCdcId(cdcId);

        return Boolean.TRUE;
    }

    @Override
    public Boolean save(CdcDatabaseGroupRequest jobConfigDO) {

        //先删除一把
        removeByCdcId(jobConfigDO.getCdcId());

        databaseConfigDaoService.save(jobConfigDO);

        List<CdcTableConfigGroupRequest> tableList = jobConfigDO.getTableList();
        if (CollUtil.isEmpty(tableList)) {
            return Boolean.TRUE;
        }
        for (CdcTableConfigGroupRequest cdcTableConfigGroupRequest : tableList) {
            cdcTableConfigGroupRequest.setId(null);
            cdcTableConfigGroupRequest.setCdcId(jobConfigDO.getCdcId());
            cdcTableConfigGroupRequest.setCdcDatabaseId(jobConfigDO.getId());
            tableConfigDaoService.save(cdcTableConfigGroupRequest);
            List<CdcColumnConfigDO> columnList = cdcTableConfigGroupRequest.getColumnList();
            if (CollUtil.isEmpty(columnList)) {
                continue;
            }
            for (CdcColumnConfigDO cdcColumnConfigDO : columnList) {
                cdcColumnConfigDO.setId(null);
                cdcColumnConfigDO.setCdcTableId(cdcTableConfigGroupRequest.getId());
                cdcColumnConfigDO.setCdcId(jobConfigDO.getCdcId());
            }
            cdcColumnConfigDaoService.saveBatch(columnList);
        }

        return Boolean.TRUE;
    }
}
